package com.xiaoxiong.mami.dao.mapper;

import com.xiaoxiong.mami.dao.daomain.UserAddress;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * 用户地址
 */
@Mapper
public interface UserAddressMapperImpl extends UserAddressMapper{

    @Select("select * from user_address where userid = #{userId} and status = 1  order by updateTime,isDef desc")
    List<UserAddress> getAddressByUserId(String userId);

    @Select("select * from user_address where userid = #{userId} and id = #{id} limit 1 ")
    UserAddress getAddressByAddressId(@Param("userId") String userId,@Param("id")  String id);

    @Update("update user_address set status = 0 where id = #{id}")
    Integer deleteAddressByAddressId(Integer id);


    @Insert({
            "insert into user_address (userid,receiver, ",
            "telephone, address, ",
            "status, createTime, ",
            "updateTime,gender,isdef)",
            "values ( #{userId,jdbcType=VARCHAR},#{receiver,jdbcType=VARCHAR}, ",
            "#{telephone,jdbcType=VARCHAR}, #{address,jdbcType=VARCHAR}, ",
            "1,now(), ",
            "now(),#{gender},#{isDef})"
    })
    int insertAddress(UserAddress record);


    @Update({
            "update user_address",
            "set userid = #{userid,jdbcType=VARCHAR},receiver=#{receiver},",
            "telephone = #{telephone,jdbcType=VARCHAR},",
            "address = #{address,jdbcType=VARCHAR},",
            "status = #{status,jdbcType=BIT},",
            "updateTime = now()",
            "where userId = #{userId}"
    })
    int updateByUserId(UserAddress record);

    @Update("update user_address set isDef =#{isDef} where id = #{id} and userId =#{userId}")
    Integer updateAddressDef(@Param("userId")String userId,@Param("id")String id,@Param("isDef")boolean isDef);

    @Update({
            "update user_address set updateTime = now(),isDef=false where userId = #{userId}"
    })
    int updateDefIsNot(String userId);




}
